﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.Entities;
using Super.Infrastructure;
using Super.DomainService;
using Super.Mvc;

namespace Super.Web.Areas.Admin.Controllers
{
    public class StockChangeTypeController : BaseController
    {
        //
        // GET: /Admin/StockChangeType/

        public ActionResult Index()
        {
            var adp = new DbContextAdapter();
            var stockchangeTypeService = new StockChangeTypeService(adp);
            var page = Query.Params("pageNum");

            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }

            var stockchangeTypes = stockchangeTypeService.Repository.Find(a => a.IsDel == false).Page(page.ToInt(1),PageSize);
            
            BindPage(stockchangeTypes.ItemCount, page.ToInt(), PageSize);
            ViewBag.Records = stockchangeTypes.ItemCount;

            return View(stockchangeTypes.ItemCollection.ToList());
        }

        public ActionResult Edit()
        {

            var id = Query.Get("id").ToInt(0);
            if (id > 0)
            {
                var adp = new DbContextAdapter();
                var stockchangeTypeService = new StockChangeTypeService(adp);
                var stockchangeTypeModel = stockchangeTypeService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(stockchangeTypeModel);
            }

            return View();
        }

        [HttpPost]
        public JsonResult Edit(StockChangeType Model)
        {
            var adp = new DbContextAdapter();
            var stockchangeTypeService = new StockChangeTypeService(adp);
            stockchangeTypeService.Repository.InsertOrUpdate(Model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "Index");
        }

    }
}
